package com.wx.payment;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import javax.validation.constraints.NotNull;
import java.io.File;

/**
 * @author: 王霄
 * @date: 2021/10/12 12:54
 **/
public class Generator {

    //数据源
    private static final String url = "jdbc:mysql://127.0.0.1:3306/springcloud";
    private static final String username = "root";
    private static final String password = "199691";
    //生成文件的作者名称
    private static final String author = "admin";
    //生成文件路径配置
    private static final String parent_path = "com.wx.payment";
    private static final String entity_path = "dao.entity";
    private static final String mapper_path = "dao.mapper";
    private static final String xml_path = "dao.xml";
    private static final String service_path = "service";
    private static final String service_impl_path = "service.impl";
    private static final String controller_path = "controller";

    //要生成的表名集合
    private static final String[] generate_tabs = new String[]{"payment"};

    public static void main(String[] args) {
        FastAutoGenerator.create(new DataSourceConfig.Builder(url, username, password))
                .globalConfig(builder -> builder
                        .outputDir(System.getProperty("user.dir") + "/payment8001/src/main/java")
                        .disableOpenDir()
                        .author(author)
                        .fileOverride()
                        .dateType(DateType.TIME_PACK)
                        .build())
                .packageConfig(builder -> builder
                        .parent(parent_path)
                        .entity(entity_path)
                        .mapper(mapper_path)
                        .xml(xml_path)
                        .service(service_path)
                        .serviceImpl(service_impl_path)
                        .controller(controller_path)
                        .build())
                .strategyConfig(builder -> builder
                        .addInclude(generate_tabs)
                        .entityBuilder()
                        .enableLombok()
                        .enableChainModel()
                        .enableRemoveIsPrefix()
                        .enableTableFieldAnnotation()
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .idType(IdType.AUTO)
                        .mapperBuilder()
                        .superClass(BaseMapper.class)
                        //.enableBaseResultMap()
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapper")
                        .serviceBuilder()
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                        .controllerBuilder()
                        .enableHyphenStyle()
                        .enableRestStyle()
                        .formatFileName("%sController")
                        .build())
                //可以指定不生成哪些类
                .templateConfig(builder -> builder
                        //.disable(TemplateType.XML)
                        .disable(TemplateType.CONTROLLER)
                        .build())
                //重写新生成的文件策略,只有entity每次都被重写
                .templateEngine(new VelocityTemplateEngine() {
                    @Override
                    protected boolean isCreate(@NotNull File file) {
                        String name = file.getName().split("\\.")[0];
                        boolean flag = name.endsWith("Mapper")
                                || name.endsWith("Service")
                                || name.endsWith("ServiceImpl")
                                || name.endsWith("Controller");
                        return !file.exists() || (getConfigBuilder().getGlobalConfig().isFileOverride() && !flag);
                    }
                })
                .execute();
    }
}
